package com.xiaohe66.web.infrastructure.mybatis.common.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohe66.web.domain.common.value.TagId;
import com.xiaohe66.web.domain.task.value.TaskId;
import com.xiaohe66.web.infrastructure.mybatis.common.mapper.TaskTagMapper;
import com.xiaohe66.web.infrastructure.mybatis.common.model.TaskTagDo;
import com.xiaohe66.web.integration.ServiceSupport;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xiaohe
 * @since 2023.01.29 13:28
 */
@Repository
public class TaskTagRepositoryImpl extends ServiceSupport<TaskTagMapper, TaskTagDo> {

    public void removeByTaskId(TaskId taskId) {

        TaskTagDo update = new TaskTagDo();
        update.setTaskId(taskId.getValue());

        remove(new QueryWrapper<>(update));
    }

    public Set<TagId> listTagIdByTaskId(TaskId taskId) {

        LambdaQueryWrapper<TaskTagDo> queryWrapper = new LambdaQueryWrapper<TaskTagDo>()
                .select(TaskTagDo::getTagId)
                .eq(TaskTagDo::getTaskId, taskId.getValue());

        List<TaskTagDo> list = list(queryWrapper);

        return list.stream()
                .map(TaskTagDo::getTagId)
                .map(TagId::new)
                .collect(Collectors.toSet());
    }
}
